<?php

class User_RegistrationController extends Zend_Controller_Action
{
    public function init()
    {
        $headLink = $this->view->headLink();
        $headLink->appendStylesheet('/css/ui.datepicker.css');

        $headScript = $this->view->headScript();
        $headScript->appendFile('/js/jquery.js');

        // Documentation for the jquery ui datepicker can be found here:
        // http://docs.jquery.com/UI/Datepicker
        // A functionnal demo can be found here:
        // http://ui.jquery.com/functional_demos/#ui.datepicker
        $headScript->appendFile('/js/jquery.ui/ui/ui.datepicker.js');
        $headScript->appendFile('/js/jquery.ui/ui/i18n/ui.datepicker-fr.js');
        $headScript->appendFile('/js/jquery.ui/ui/i18n/ui.datepicker-de.js');
        $headScript->appendScript("
            $(document).ready(function() {
                $('#birthdate').datepicker($.extend({},
                $.datepicker.regional['fr'], {
                    dateFormat: 'yy-mm-dd',
                    yearRange: '-120:+0',
                    showOn: 'both',
                    buttonImage: '/images/calendar.gif',
                    buttonImageOnly: true
                }));
            })
        ");
    }

    public function indexAction()
    {
        $this->view->form = new Model_Form_Registration();
    }

    public function submitAction()
    {
        $request = $this->getRequest();

        if (!$request->isPost()) {
            return $this->_forward('index');
        }

        $form = new Model_Form_Registration();

        // Failed validation, redisplay form
        if (!$form->isValid($request->getPost())) {
            $this->view->form = $form;
            return $this->render('index');
        }

        $user = new Model_User();
        $id = $user->insert($request->getPost());

        if ($form->avatar->isUploaded()) {
            $fileInfo = $form->avatar->getFileInfo();
            $fileExtenstion = $this->_helper->FileExtension($fileInfo['avatar']);
            $fileName = $id . '.' . $fileExtenstion;
            $filePath = Zend_Registry::get('config')->paths->avatar . '/' . $fileName;

            $form->avatar->addFilter('Rename', array(
                'target' => $filePath,
                'overwrite' => true));

            // File received failed
            if (!$form->avatar->receive()) {
                $this->view->form = $form;
                return $this->render('index');
            }

            // Inform the db that the user has an avatar
            $user->update(array('has_avatar' => 1), 'id = ' . $id);
        }

        $form->setDescription('Validation passed !');
        $this->view->form = $form;

        return $this->render('index');
    }

    public function confirmationAction()
    {
        $key = $this
                ->getRequest()
                ->getParam('key');

        $user = new Model_User();
        $ret = $user->updateConfirmedUser(array(
                    'email_confirmation_key' => $key,
                    'has_confirmed_email' => 1,
                    )
                );

        $this->view->key = $key;
        $this->view->confirmationSucceeded = $ret;
    }
}
